package com.efast.dmom.aps.datahandlers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.Plant;
import com.efast.dmom.aps.dao.PlantDao;
import com.efast.dmom.aps.entity.PlantEntity;
import com.efast.dmom.aps.util.TimeUtil;
@Component
public class PlantDataHandler {

	@Autowired
	private PlantDao plantDao;

	public void save(Plant obj) {
		if (obj.getInnerData() != null) {
		obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
		obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//		obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//		obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowStatus(1);
		obj.getInnerData().setRowTrxId(111);
		plantDao.save(obj.getInnerData());
		}
	}

	public void update(Plant obj) {
		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			plantDao.update(obj.getInnerData());
		}

	}
	
	public void delete(Plant obj) {
		if(obj.getInnerData() != null){

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			// obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			
			plantDao.delete(obj.getInnerData());
		}
	}

	public Plant getByCode(String plantCode) {
		Plant plant = new Plant();
		plant.setInnerData(plantDao.getByCode(plantCode));
		return plant;
	}

	public Plant getById(long id) {
		Plant plant = new Plant();
		plant.setInnerData(plantDao.getById(id));
		return plant;
	}

	public Plant getByName(String name) {
		Plant plant = new Plant();
		plant.setInnerData(plantDao.getByName(name));
		return plant;
	}

	public List<Plant> getAllPlant(){
		List<PlantEntity> allPlant = plantDao.getAllPlant();
		List<Plant> plants = new ArrayList<Plant>();
		if(allPlant != null && allPlant.size() > 0) {
		for(PlantEntity plantEntity:allPlant) {
			Plant plant = new Plant();
			plant.setInnerData(plantEntity);
			plants.add(plant);
		}
		return plants;
	}
		return null;
	}
	
	public List<Map<String,String>> queryPlantCodeIdNameList(){
		return plantDao.queryPlantCodeIdNameList();
	}

}
